package com.xhwl.logistics.service.impl.http;


import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.exception.ApplicationException;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.constant.KeTopGeoConstant;
import com.xhwl.logistics.dto.http.CarportFreeKeTopGeoDTO;
import com.xhwl.logistics.dto.http.ParkingLotInfoKeTopGeoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 对接科拓地磁感应接口
 */
@Slf4j
//@Service
public class KeTopGeoHttpServiceImpl {

    /**
     * 楼层和区域的默认值
     */
    private static final Integer floorAndArea = 0;

    /**
     * 停车场信息查询接口
     */
    public ParkingLotInfoKeTopGeoDTO getKeTopGeoParkingInfo(VehicleIntegratedConfigBO vehicleIntegratedConfigBO){
        HashMap<String, Object> params = new HashMap<>();
        long ts = new Date().getTime();
        String cmd = KeTopGeoConstant.VEHICLE_KETOP_PARKING_INFO.substring(
                KeTopGeoConstant.VEHICLE_KETOP_PARKING_INFO.lastIndexOf("/") + 1, KeTopGeoConstant.VEHICLE_KETOP_PARKING_INFO.length());
        params.put("cmd",cmd);
        params.put("ts",ts);
        params.put("appCode",vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getAppIdTwo());
        params.put("sign",getSign(cmd,ts,vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getAppIdTwo(),
                vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getAppSecretTwo()));
        JSONObject resultObject = post(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getHostTwo() + KeTopGeoConstant.VEHICLE_KETOP_PARKING_INFO, params);
        return JSONObject.parseObject(keTopGeoResponseHandler(resultObject), ParkingLotInfoKeTopGeoDTO.class);
    }

    /**
     * 空余车位数查询接口
     */
    public List<CarportFreeKeTopGeoDTO> getKeTopGeoFreeCarspot(VehicleIntegratedConfigBO vehicleIntegratedConfigBO){
        HashMap<String, Object> params = new HashMap<>();
        long ts = new Date().getTime();
        String cmd = KeTopGeoConstant.VEHICLE_KETOP_FREE_CARSPOT.substring(
                KeTopGeoConstant.VEHICLE_KETOP_FREE_CARSPOT.lastIndexOf("/") + 1, KeTopGeoConstant.VEHICLE_KETOP_FREE_CARSPOT.length()
        );
        HashMap<String, Object> keTopGeoFloorArea = new HashMap<>();
        keTopGeoFloorArea.put("areaId",floorAndArea);
        keTopGeoFloorArea.put("floorId",floorAndArea);

        params.put("cmd",cmd);
        params.put("ts",ts);
        params.put("appCode",vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getAppIdTwo());
        params.put("sign",getSign(cmd,ts,vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getAppIdTwo(),
                vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getAppSecretTwo()));
        params.put("data",keTopGeoFloorArea);
        JSONObject resultObject = post(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getHostTwo() + KeTopGeoConstant.VEHICLE_KETOP_FREE_CARSPOT, params);
        return JSONObject.parseArray(keTopGeoResponseHandler(resultObject), CarportFreeKeTopGeoDTO.class);
    }

    /**
     * 对科拓地磁感应返回的结果进行判断
     */
    private String keTopGeoResponseHandler(JSONObject resultObject){
        String success = resultObject.getString("success");
        if(!StringUtils.equals(success,"true")){
            throw new BusinessException(resultObject.getString("msg"));
        }
        return resultObject.getString("content");
    }

    /**
     * 请求科拓地磁感应接口
     */
    private JSONObject post(String path, Map<String, Object> params){
        log.info("【KeTopGeo post】path:{},params:{}",path,JSONObject.toJSONString(params));
        String result = null;
        try {
            result = HttpRequest
                    .post(path)
                    .body(JSONObject.toJSONString(params))
                    .execute()
                    .body();
        }catch (Exception e){
            log.error("【KeTopGeo post】path:{},params:{};request is failed!",path,JSONObject.toJSONString(params));
            throw new ApplicationException();
        }
        if (StringUtils.isEmpty(result)){
            log.error("【KeTopGeo post】path:{},params:{};result is null!",path,JSONObject.toJSONString(params));
            throw new BusinessException();
        }
        log.info("【KeTopGeo post】path:{},params:{};result:{}",path,JSONObject.toJSONString(params),result);
        return JSONObject.parseObject(result);
    }

    /**
     * 构建科拓地磁感应的sign值
     */
    private String getSign(String cmd, long ts, String appId, String appSecret){
        String preSign = cmd + ts + appId + appSecret;
        return DigestUtils.md5DigestAsHex(preSign.getBytes()).toLowerCase();
    }
}
